﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Pong
{
    class Ball
    {
        Texture2D texture;
        Vector2 position;
        Rectangle size;
        Vector2 velocity;
        SoundEffect soundEffect;
        Random random = new Random();
        private GameWindow Window;
        private Texture2D texture2D;
        private Vector2 vector2;


        public Ball( GameWindow janela , Texture2D texture, Vector2 position,SoundEffect soundEffect)//<CONSTRUTOR!!!
        {
            this.texture = texture;//AQUI
            this.size = texture.Bounds;//AQUI
            this.position = new Vector2(
                janela.ClientBounds.Width/2
                - this.size.Width/2 ,
                janela.ClientBounds.Height/2
                - this.size.Height/2);//AQUI
            this.velocity = new Vector2(0, 5);//AQUI

            float positionX = random.Next(-5,5);
            float positionY = random.Next(-5,5);

            this.velocity = new Vector2(positionX, positionY);

            
        }

        public Ball(GameWindow Window, Texture2D texture2D, Vector2 vector2)
        {
            // TODO: Complete member initialization
            this.Window = Window;
            this.texture2D = texture2D;
            this.vector2 = vector2;
        }

        public void Update ()
        {
             
        }


        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, position, Color.White);

        }

        public void ManterNaTela(GameWindow janela)
        {
            if (position.Y <= 0)
            {
                velocity.Y = -velocity.Y;
                soundEffect.Play();
            }

            if (position.X <= 0 )
            {
                velocity *= -1;
                soundEffect.Play();
            }

            if(position.Y >=janela.ClientBounds.Height-size.Height)
            {
                velocity.Y*=-1;
                soundEffect.Play();
            }

            if(position.X >= janela.ClientBounds.Width - size.Width)
            {
                velocity.X *= -1;
                soundEffect.Play();
            }

            #region Manter na Janela

            if (position.Y <= 0)
            {
                position.Y = 0;
            }

            if (position.Y >= janela.ClientBounds.Height - size.Height)
            {
                position.Y = janela.ClientBounds.Height - size.Height;
            }

            #endregion



        }
        
    }

}
